var file_id = $.query.get('file_id');
var target_id= $.query.get('target_id');
var param={target_id:target_id,file_id:2};
loadImage();
//document ready
$(function(){
	
	/*$('#removeImageButton').bind('click', grid2.remove);
	$('#saveButton').bind('click', grid2.accept);//保存datagrid绑定事件
	$('#rejectButton').bind('click', grid2.rejectChanges);//撤销编辑绑定事件
*/});
//加载图片
function loadImage(){
	$("#image-list").empty();
	$._post("/authenticated/ajax/grid_sys_pub_file_item_target_rel",JSON.stringify(param),function(result){
		if(result.success){
			var dataList = result.data;
			if(dataList.length>0){
				for(var i=0;i<dataList.length;i++){
					var _li = dataList[i];
					$("#image-list").append('<li>'
		     				 +' <img alt="" src="'+_li.access_url+'">'
		 					 +' <div class="image-title">'+_li.original_file_name+'</div></li>');
				}
				var single=false;
				var i=0;
				 $("ul.selectMonth li").click(function() {

					 if(!$(this).hasClass("active")){
					 		i = 0;
					 	}
							 if(i==0){
								 i=1;
								 if(single){
										$("ul.selectMonth li").each(function(){	
											 $(this).removeClass("active");
										});
									}
								    $(this).addClass("active");
								    selectNewImg.length=0;
								    listNewImg="";
								    listNewImgUrl="";
									    $("ul.selectMonth li.active").each(function(){
									    	listNewImg+=$(this).attr("data-file")+",";
									    	listNewImgUrl+=$(this).attr("data-fileUrl")+",";
									    	selectNewImg.push($(this).attr("data-file"));
									    });
								    listNewImg=listNewImg.substring(0,listNewImg.length-1);
								    if(selectNewImg.length>0){
								    	$(".selected-count-region").removeClass("hidden");
								    	$(".js-selected-count").html(selectNewImg.length);
								    	$("#saveImages").removeAttr("disabled");
								    }else{
								    	$(".selected-count-region").addClass("hidden");
								    	$("#saveImages").attr("disabled","disabled");
								    }
								    
							    }else{
							     i=0;
								 $(this).removeClass("active");
								 selectNewImg.length=0;
								 listNewImg="";
								 listNewImgUrl="";
								 $("ul.selectMonth li.active").each(function(){
									 listNewImg+=$(this).attr("data-file")+",";
									 listNewImgUrl+=$(this).attr("data-fileUrl")+",";
									 selectNewImg.push($(this).attr("data-file"));
								    });
								 listNewImg=listNewImg.substring(0,listNewImg.length-1);
								 if(selectNewImg.length>0){
								    	$(".selected-count-region").removeClass("hidden");
								    	$(".js-selected-count").html(selectNewImg.length);
								    	$("#saveImages").removeAttr("disabled");
								    }else{
								    	$(".selected-count-region").addClass("hidden");
								    	$("#saveImages").attr("disabled","disabled");
								    }
								 }
				 });
			}
			
		}else{
			$.messager.alert("提示", "加载失败！<br>"+result.message);
		}
	});
}